package com.boyi.center.controller.impl;

import com.boyi.center.controller.BaseController;
import com.boyi.shared.common.Page;
import com.boyi.shared.common.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.List;

/**
 * RestTemplateControllerImpl.class
 *
 * @author tingwen
 * @date date
 */
@RestController
@RequestMapping("restfulTest")
public class RestTemplateControllerImpl implements BaseController {
    private final static String baseUrl = "http://microcloud-provider-product/user/";
    private final static String save = baseUrl + "save";
    private final static String batchInsert = baseUrl + "batchInsert";
    private final static String delete = baseUrl + "delete";
    private final static String updateNotNull = baseUrl + "updateNotNull";
    private final static String selectPage = baseUrl + "selectPage";
    private final static String batchDelete = baseUrl + "batchDelete";
    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private HttpHeaders httpHeaders;
    // 用来获取服务端的相关信息
    @Autowired
    private LoadBalancerClient loadBalancerClient;

    @GetMapping
    @Override
    public Result<List> selectPage(Object entity, Page page) {
        ServiceInstance serviceInstance = loadBalancerClient.choose("microcloud-provider-product");
        System.out.println(
                "【*** ServiceInstance ***】host = " + serviceInstance.getHost()
                        + "、port = " + serviceInstance.getPort()
                        + "、serviceId = " + serviceInstance.getServiceId());
        return restTemplate.exchange(selectPage, HttpMethod.GET, new HttpEntity<>(httpHeaders), Result.class, entity).getBody();
    }

    @PostMapping
    @Override
    public Result<List> save(Object entity) {
        return restTemplate.exchange(save, HttpMethod.GET, new HttpEntity<>(httpHeaders), Result.class, entity).getBody();
    }

    @Override
    public Result<List> batchInsert(List entities) {
        return restTemplate.exchange(batchInsert, HttpMethod.GET, new HttpEntity<>(httpHeaders), Result.class, entities).getBody();
    }

    @DeleteMapping
    @Override
    public Result<List> delete(Object entity) {
        return restTemplate.exchange(delete, HttpMethod.GET, new HttpEntity<>(httpHeaders), Result.class, entity).getBody();
    }

    @PutMapping
    @Override
    public Result<List> updateNotNull(Object entity) {
        return restTemplate.exchange(updateNotNull, HttpMethod.GET, new HttpEntity<>(httpHeaders), Result.class, entity).getBody();
    }

    @PatchMapping
    @Override
    public Result<List> batchDelete(String[] ids) {
        return restTemplate.exchange(batchDelete, HttpMethod.GET, new HttpEntity<>(httpHeaders), Result.class, ids).getBody();
    }
}
